package com.ruoyi.common.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.core.env.Environment;

import javax.annotation.Resource;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author 立勤
 * @date 2023/07/12 15:39:23
 */
@Component
public class RunConfig {

    @Resource
    private Environment environment;

    // 在启动完成后输出IP和端口
    @EventListener(ApplicationReadyEvent.class)
    public void afterStartup() throws UnknownHostException {
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = environment.getProperty("server.port");
//        String property = environment.getProperty("server.servlet.context-path");
//        String path = property == null ? "" :  property;
        String nwIp = environment.getProperty("xytp.nw-ip");
        String nwPort = environment.getProperty("xytp.nw-port");
        System.out.println(
            "\n\t" +
                "----------------------------------------------------------\n\t" +
                "校园投票后端启动成功 :\n\t" +
                "Local: \t\thttp://localhost:" + port + "/\n\t" +
                "External: \thttp://" + ip + ":" + port + "/\n\t" +
                "内网穿透: \thttp://" + nwIp + ":" + nwPort + "/\n\t" +
                "------------------------------------------------------------");
    }
}
